﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Quaack
{
    public partial class nieuwbericht : System.Web.UI.Page
    {
        protected void page_preinit()
        {
            if (User.Identity.IsAuthenticated) Page.MasterPageFile = "~/Site-Ingelogd.Master";
            else
            {
                Page.MasterPageFile = "~/Site.Master";
                string url;
                url = "~/default.aspx";
                Response.Redirect(url);
            }
            

        }
        protected void messagegrid(object sender, EventArgs e)
        {

        }

        protected void grdUserMessages_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void sqlUserMessageSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated) 
            {
                String user = User.Identity.Name;
                int userID = 0;
                int huidigesender = 0;
                SqlConnection sqlConn = new SqlConnection(
                "Data Source=.\\sqlexpress;Initial Catalog=Quaack;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");

                /*   Profileid opvragen van de ingelogde gebruiker   */
                SqlCommand GetUserID = new SqlCommand("SELECT profileid from profile where username = '" + user + "'", sqlConn);

                /*   open database, haal de userID op en sluit database   */
                sqlConn.Open();
                object sqlObject = GetUserID.ExecuteScalar();
                if (sqlObject != null)
                {
                    // userID zal uiteindelijk gebruikt worden om de sender aan te geven in de message tabel
                    userID = Convert.ToInt32(sqlObject.ToString());
                    huidigesender = userID;
                }
                sqlConn.Close();
                sqlUserMessageSource.SelectCommand = "SELECT messid, messdatetime, sender, message FROM message WHERE sender = " + userID + "";
            }
        }

        protected void PlaatsBericht(object sender, EventArgs e)
        {
            int userID = 0;

            if (User.Identity.IsAuthenticated)
            {
                DateTime posttimemessage = DateTime.Now;
                String message = txtNieuwbericht.Text;
                //String user = Membership.GetUser().UserName;
                String user = User.Identity.Name;
                int removed = 0;
                SqlConnection sqlConn = new SqlConnection(
                "Data Source=.\\sqlexpress;Initial Catalog=Quaack;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");

                /*   Profileid opvragen van de ingelogde gebruiker   */
                SqlCommand GetUserID = new SqlCommand("SELECT profileid from profile where username = '" + user + "'", sqlConn);
                
                /*   open database, haal de userID op en sluit database   */
                sqlConn.Open();
                object sqlObject = GetUserID.ExecuteScalar();
                if ( sqlObject != null)
                {
                    // userID zal uiteindelijk gebruikt worden om de sender aan te geven in de message tabel
                    userID = Convert.ToInt32(sqlObject.ToString());
                }
                sqlConn.Close();

                /*   bericht opslaan   */
                SqlCommand plaatsBericht = new SqlCommand("Insert into message(messdatetime, sender, message, removed) values (@messdatetime, @sender, @message, @removed)", sqlConn);

                plaatsBericht.Parameters.AddWithValue("@messdatetime", posttimemessage);
                plaatsBericht.Parameters.AddWithValue("@sender", userID); 
                plaatsBericht.Parameters.AddWithValue("@message", message);
                plaatsBericht.Parameters.AddWithValue("@removed", removed);

                sqlConn.Open();
                plaatsBericht.ExecuteNonQuery();
                sqlConn.Close();

                string url;
                url = "~/EigenPagina.aspx";
                Response.Redirect(url);
            }

            if (IsPostBack)
            {
                // Query voor de datasource aanpassen zodat alleen de berichten van de huidige gebruiker getoond worden.
                sqlUserMessageSource.SelectCommand = "SELECT messid, messdatetime, sender, message FROM message WHERE sender = " + userID + "";
                
                // Reload DataGrid
                grdUserMessages.DataBind();

                txtNieuwbericht.Text = "";
                //Response.Redirect(Request.RawUrl);
            }
        }
    }
}